<?php
			
	/* SVN FILE: $Id: view.php 61 2011-01-23 22:08:36Z andreoletti $ */
	
	/**
	 * Project Name : arcadia
	 *
	 * @author $Author: andreoletti $
	 * @version $Revision: 61 $
	 * @lastrevision $Date: 2011-01-23 23:08:36 +0100 (dom, 23 gen 2011) $
	 * @filesource $URL: http://svn.fletchlynd.com/arcadia/trunk/visioni/view.php $
	 */

	require_once 'include/functions.php';
	require_once 'include/const.inc.php';
	require_once '../include/auth.inc.php';
	require_once '../include/conn.inc.php';

	require_once('../include/view.php');
	
	// sblocco la modifica dei campi della tabella sopralluogo
	$aFields = array('dtVerifica','iVerifica','iReferente','sNoteVerifica');
	foreach ($aFields as $sField) {
		if (!is_null(fnReadSessionValue($sField, 'AccessLvl', $sAPP, 'field', true))) {
			fnSaveSessionValue(fnReadSessionValue($sField, 'AccessLvl', $sAPP, 'field', true), $sField, 'AccessLvl', 'field');
			fnUnsetSessionValue($sField, 'AccessLvl', $sAPP, 'field', true);
		}
	}
	
	if (!empty($iView)) {
	  	$sHeaderTitle = $sAPP_NAME." &raquo; ".$aData['sFornitore']." &raquo; ".$aData['sDescrizione'];
		$sTableTitle = "<div style='font-size: 0.7em'>".$aData['sFornitore']."</div><div>".$aData['sDescrizione']."</div>";
			} else  {
		$sHeaderTitle = $sAPP_NAME." &raquo; $sTableTitle";
	}

	if (isset($aData['iRinnovo']) && !empty($aData['iRinnovo'])) {
		$html .= "<div class='ui-state-highlight ui-corner-all' style='margin-top:4px; margin-bottom:6px; margin-left:5px; padding:4px;'>".
				"<img src='../themes/common/images/clock.png' width='16px' height='16px' style='float:left;'>
				<div style='margin-left: 25px;'><strong>autorizzazione prorogata</strong>&nbsp;
				<a href='$sViewItemLink?iView=".$aData['iViewRinnovo'].($sAction=='edit' || $sAction=='update' ? "&action=edit" : "")."'>&raquo; visualizza i dati della proroga</a>&nbsp;&nbsp;".		
				"</div></div>";
	}
			
	$html .= "<div id='tabs'><ul>$sTabsHtml</ul>";
	
	$i = 1;
	if (is_allowed('view', fnFieldAccessLvl($iAccessLvl, $i))) {
		
		$html .= "<div id='tabs-$i'>";
			
			$html .= ($sAction == 'edit' ? "<form method='post' id='form-tab-$i' class='buttons' action='transact.php'>" : '');
			$html .= "<table class='standard' border='".fnTableBorder()."' width='98%'>";
			
				$html .= "<col width='25%'><col width='25%'><col width='25%'><col width='25%'>";	
				
				$html .= "<tbody>";
				$html .= "<tr>	<td colspan='2'>".fnTextInput('sFornitore','fornitore', $aData, 45)."</td>";
				$html .= "		<td colspan='2' rowspan='".(isset($aData['tsMailScadenza']) && $aData['tsMailScadenza']!='0000-00-00 00:00:00' ? '10' : '9')."'>";
				
			
					$html .= "<table class='table-ingegneria' border='".fnTableBorder()."' width='90%'>";
						$html .= "<tbody>";
						$html .= "<col width='50%'><col width='50%'>";	
						$html .= "<tr><td>".fnTextInput('dtVerifica', 'data sopralluogo', $aData)."</td>";
						$html .= "<td>".fnSelectInput('iVerifica', 'esito', $aData, fnSelOptionsFromResources('ESITO-VERIFICA', '1', 'iVerifica'),'index')."</td></tr>";	
						$html .= "<tr><td colspan='2'>".fnSelectInput('iReferente', 'referente', $aData, fnSelOptionsFromResources('SIC', '1', 'iReferente'), 'index')."</td></tr>";	
		
						// BOTTONE INVIO SEGNALAZIONE EMAIL ** INIZIO **
						
						$html .= "<tr><td>&nbsp;</td><td>";
						
						$bEmailButtonShow = true;
						
						if (!empty($iView) && !empty($aData['dtVerifica']) && !empty($aData['iVerifica']) && !empty($aData['iReferente']) && $sAction!='edit') {
							
							$sQuery = fnGetResourcesQuery('ESITO-VERIFICA', array('sMailTo'), 1);
							$rResult = custom_mysql_query($sQuery);
							while ($aRow = mysql_fetch_array ($rResult)) {
								if (!empty($aRow['sMailTo']))
									$aMailTo[$aRow['sMailTo']][] = $aRow['id'];
							}
							
							$html .= "<div id='send-email-feedback' class='small-text'></div>";
							
							// BOTTONE INVIO E-MAIL MAGAZZINO (DA PARTE DI ING CLINICA)
							if (isset($aData['tsMailMAGAZZINO']) && $aData['tsMailMAGAZZINO']=='0000-00-00 00:00:00' && is_allowed('edit', fnFieldAccessLvl($iAccessLvl, $i, 'tsMailMAGAZZINO'))) {	
								if ($bEmailButtonShow && in_array($aData['iVerifica'], $aMailTo['MAGAZZINO'])) {							
									$html .= "<br/>";
									$html .= "<span class='send-email buttons small-text'>";
									$html .= "	<button sAction='send-email' sField='tsMailMAGAZZINO' sEmailTemplate='VISIONI-MAGAZZINO' sRecipient='MAGAZZINO' sCarbonCopy='SIC' iView='$iView' sViewItemLink='$sViewItemLink' id='send-email'>e-mail magazzino tecnico</button>";
									$html .= "</span>";
									$bEmailButtonShow = false;
								}
							} else if (is_allowed('view', fnFieldAccessLvl($iAccessLvl, $i, 'tsMailMAGAZZINO'))) {
								$html .= "<span class='small-text'>Email a MAGAZZINO: ".fnHtmlTimestamp($aData['tsMailMAGAZZINO'])."</span><br/>";
							}

							// BOTTONE INVIO E-MAIL AFFARI GENERALI (DA PARTE DI ING CLINICA)
							if (isset($aData['tsMailAAGG']) && $aData['tsMailAAGG']=='0000-00-00 00:00:00') {
								if ($bEmailButtonShow && in_array($aData['iVerifica'], $aMailTo['AAGG']) && is_allowed('edit', fnFieldAccessLvl($iAccessLvl, $i, 'tsMailAAGG'))) {							
									$html .= "<br/>";
									$html .= "<span class='send-email buttons small-text'>";
									$html .= "	<button sAction='send-email' sField='tsMailAAGG' sEmailTemplate='VISIONI-AAGG' sRecipient='AAGG' sCarbonCopy='SIC' iView='$iView' sViewItemLink='$sViewItemLink' id='send-email'>e-mail affari generali</button>";
									$html .= "</span>";
									$bEmailButtonShow = false;
								}
							} else if(is_allowed('view', fnFieldAccessLvl($iAccessLvl, $i, 'tsMailAAGG'))) {
								$html .= "<span class='small-text'>Email a AAGG: ".fnHtmlTimestamp($aData['tsMailAAGG'])."</span><br/>";
							}
							
							// BOTTONE INVIO E-MAIL UNITA' (DA PARTE DI AA.GG.)
							if (isset($aData['tsMailUNITA']) && $aData['tsMailUNITA']=='0000-00-00 00:00:00') {
								if ($bEmailButtonShow && in_array($aData['iVerifica'], $aMailTo['AAGG']) && isset($aData['tsMailAAGG']) && $aData['tsMailAAGG']!='0000-00-00 00:00:00' && is_allowed('edit', fnFieldAccessLvl($iAccessLvl, $i, 'tsMailUNITA'))) {														
									$html .= "<br/>";
									$html .= "<span class='send-email buttons small-text'>";
									$html .= "	<button sAction='send-email' sField='tsMailUNITA' sEmailTemplate='VISIONI-UNITA-EXPIRED' sRecipient='UNITA' sCarbonCopy='AAGG|SIC|SEGRETERIA-DMP' iView='$iView' sViewItemLink='$sViewItemLink' id='send-email'>e-mail unità</button>";
									$html .= "</span>";
									$bEmailButtonShow = false;
								}
							} else if (is_allowed('view', fnFieldAccessLvl($iAccessLvl, $i, 'tsMailUNITA'))) {
								$html .= "<span class='small-text'>Email a UNITA: ".fnHtmlTimestamp($aData['tsMailUNITA'])."</span><br/>";
							}
							
						}
			
						$html .= "</td></tr>";
						
						// BOTTONE INVIO SEGNALAZIONE EMAIL ** FINE **
						
						if (empty($iView) || (!empty($iView) && $sAction != 'edit' && !empty($aData['sNoteVerifica'])) || (!empty($iView) && $sAction == 'edit'))	
							$html .= "<tr><td colspan='2'>".fnTextareaInput('sNoteVerifica', 'note sopralluogo', $aData)."</td></tr>";	
						$html .= "<tr><td colspan='2'>&nbsp;</td>";
						$html .= "<tbody>";
					$html .= "</table>";
					$html .= "<br/>";
					
					$html .= "<table class='table-approvvigionamenti' border='".fnTableBorder()."' width='90%'>";
						$html .= "<col width='50%'><col width='50%'>";	
						$html .= "<tr><td>".fnTextInput('sBollaIn','n. bolla ingresso',$aData,12)."</td><td>".fnTextInput('dtIngresso', 'data ingresso', $aData)."</td></tr>";	
						$html .= "<tr><td>".fnTextInput('sBollaOut','n. bolla uscita',$aData,12)."</td><td>".fnTextInput('dtOut', 'data uscita', $aData)."</td></tr>";	
						if (empty($iView) || (!empty($iView) && $sAction != 'edit' && !empty($aData['sNoteMagazzino'])) || (!empty($iView) && $sAction == 'edit'))	
							$html .= "<tr><td colspan='2'>".fnTextareaInput('sNoteMagazzino', 'note magazzino', $aData)."</td></tr>";	
						$html .= "<tr><td colspan='2'>&nbsp;</td></tr>";
					$html .= "</table>";
					$html .= "<br/>";
							
					$sFindLinked = fnFindLinked($iView);
					if (isset($sFindLinked) && !empty($sFindLinked)) {
						$html .= "<table class='table-approvvigionamenti' border='".fnTableBorder()."' width='85%'>";
							$html .= "<col width='50%'><col width='50%'>";	
							$html .= "<tr><td colspan='2'><strong>Cronologia Autorizzazioni &amp; Proroghe</strong></td></tr>";	
							$html .= "<tr><td colspan='2'>$sFindLinked</td></tr>";	
						$html .= "</table>";
				
						$html .= "</td></tr>";
					}
					
				$html .= "</td></tr>";
				
				$html .= "<tr><td colspan='2'>".fnTextInput('sUnita', 'sUnita', $aData, 45)."". "</td></tr>";
				$html .= "<tr><td colspan='2'>".fnTextInput('sUnitaEmail', 'indirizzo e-mail unità (responsabile o segreteria)', $aData, 45)."</td></tr>";
				$html .= "<tr><td colspan='2'>".fnTextareaInput('sDescrizione', 'descrizione', $aData, '95%', '4.5em', false)."</td></tr>";
				$html .= "<tr><td>".fnTextInput('sProtocollo','protocollo',$aData,12)."</td><td>".fnTextInput('dtProtocollo', 'protocollo del', $aData)."</td></tr>";	
				$html .= "<tr><td>".fnTextInput('dtInizio', 'data inizio', $aData)."</td><td>".fnTextInput('dtScadenza', 'data scadenza', $aData)."</td></tr>";	
				
				if (isset($aData['tsMailScadenza']) && $aData['tsMailScadenza']!='0000-00-00 00:00:00')
					$html .= "<tr><td>&nbsp;</td><td>".fnHtmlTimestamp($aData['tsMailScadenza'], true, 'Invio e-mail promemoria scadenza')."</td></tr>"; 		
				
				$html .= "<tr><td>&nbsp;</td><td>".fnCheckbox('iArchiviato','archiviato (vedi note) <img src=\'images/folder.png\' height=\'16\' width=\'16\'>',$aData)."</td></tr>";	
				
				if (empty($iView) || (!empty($iView) && $sAction != 'edit' && !empty($aData['sNote'])) || (!empty($iView) && $sAction == 'edit'))	
					$html .= "<tr><td colspan='2'>".fnTextareaInput('sNote', 'note', $aData, '95%', '10em', false)."</td></tr>"; 
 		
				$html .= "</tbody>";
			$html .= "</table>";
			
			if ($iView != 0 && $sAction != 'edit') { 
				$html .= "<h5>".fnHtmlLastModify($aData['tsLastModify'])."</h5>";
			} else {
				$html .= "<h5>&nbsp;</h5>";
			}

			$html .= fnButtons($iView, $i, $iAccessLvl, $aData);
			
			// PULSANTE 'PROROGA AUTORIZZAZIONE'
			if (!empty($iView) && $aData['iRinnovo']!=1 && $aData['iTrash']!=1 && ($sAction!='edit' && $sAction!='update') && is_allowed('edit',$iAccessLvl)) {
				$html .= "<div style='float:left; margin-left:10px;'>";
				$html .= "<form name='extend' class='buttons' method='post' action='transact.visioni.php'>";
				$html .= "<input type='hidden' name='sViewItemLink' value='$sViewItemLink'>";
				$html .= "<input type='hidden' name='iTab' value='$i'>";
				$html .= "<input type='hidden' name='iView' value='$iView'>";
				$html .= "<input type='hidden' name='sAction' value='extend'>";
				$html .= "<input type='submit' value='Proroga autorizzazione'>";				
				$html .= "</form></div>";
			}
			
		$html .= "</div>";
	
	}
	
	$i = $iTabUploads;
	if (is_allowed('view', fnFieldAccessLvl($iAccessLvl, $sViewItemLink, $i))) { 
		
		$html .= "<div id='tabs-$i'>";			
			$html .= fnUploadsTabContent($iView, $sViewItemLink, $i);
		$html .= "</div>";
	
	}
		
	$html .= "</div>";
	
	$html .= "<div id='send-email-dialog' title='Attenzione'>";
	$html .= "<p>Per inviare l'e-mail di segnalazione occorre inserire un indizzo email nel campo <em>indirizzo e-mail unità</em></p>";
	$html .= "</div>";
	
	$html .= "<div id='archivio-dialog' title='Conferma Archiviazione'>";
	$html .= "<p>Archiviare la prova visione?</p>";
	$html .= "</div>";
	
	// contenuto <head> della pagine
	$sHeadContent .= fnViewHeadContent($iTab, $aDisabledTabs);
	
	$bSEARCH_FORM_SHOW = false; // il form di ricerca non viene visualizzato nello script $sViewItemLink
								// non posso assegnare subito questo valore a $bSEARCH_FORM_SHOW perchè
								// alcune funzioni in ../include/view.php utilizzano questa variabile.
								
	require_once('../themes/'.$sTHEME.'/template.php');
	die();




	function fnViewHeadContent ($iTab, $aTabsDisabled) {

		global $sSUITE_NAME, $sAPP;
		
		global $iView, $sViewItemLink;
		
		global $sAction;
		
		global $sJsDocumentReady;
		
		global $aTabsLabels;
		
		$sTabsDisabled = '';
		foreach ($aTabsDisabled as $i) { $sTabsDisabled .= "$i, "; }
		$sTabsDisabled = ($sTabsDisabled!='' ? " disabled: [".substr_replace( $sTabsDisabled, "", -2 )."] " : '');

		$iTabSelected = $i = 0;
		foreach ($aTabsLabels as $sKey => $sValue) {
			if ($sKey == $iTab) { $iTabSelected = $i; }
			$i++;
		}
		
		$sSelectedTab = ($iTabSelected!=0 && !in_array($iTabSelected, $aTabsDisabled) ? ($sTabsDisabled!="" ? ", " : '') . " selected: $iTabSelected " : "");

		$sHeadContent = "
		
			<script type='text/javascript' charset='utf-8'>
	
				$().ready(function() {
				
					var oClicked = null;
					
					$sJsDocumentReady
					
					// validate form form on keyup and submit
					$('#sendExpiredEmail').validate({
						rules: {
							unita_email: { required: true, email: true }
						}
					});
														
					$('#tabs').tabs({ $sTabsDisabled $sSelectedTab });

					$('button, input:submit', '.divSendEmail').button({
			            icons: {
			                primary: 'ui-icon ui-icon-mail-closed'
			            }
        			});
					
        			$('button, input:submit', '.buttons').button();
					
					$('#send-email-dialog').dialog( {
						autoOpen: false,
						resizable: false,
						modal: true,
						buttons: {
							Ok: function() {
								$(this).dialog('close');
							}
						}
					});
					
					$('.send-email button').click(function() {
						$.getJSON('sendmail.php', {	'sAction': $(this).attr('sAction'),
													'sField': $(this).attr('sField'),
													'sEmailTemplate': $(this).attr('sEmailTemplate'),
													'sRecipient': $(this).attr('sRecipient'),
													'sCarbonCopy': $(this).attr('sCarbonCopy'),
													'iView': $(this).attr('iView'),
													'sViewItemLink': $(this).attr('sViewItemLink') },  function (json) {
								if (json[0].sScript=='sendmail.php' && json[0].sError=='') {
									$('#send-email-feedback').html(json[0].sHtml);
									$('.prettydate').prettyDate();
								} else {
									alert(json[0].sError);
								}
						});
						$(this).hide();
					}); 
					
					$('#iArchiviato').click(function() {
						oClicked = $(this);
						src = oClicked.attr('src'),
							onCheck = /\_on\.png$/,
							offCheck = /\_off\.png$/;
						if (oClicked.attr('src').match(offCheck)) {
							$('#archivio-dialog').dialog('open');
						} else if ($(this).attr('src').match(onCheck)) { 
							fnToggle(oClicked);
						}
					});
					
					$('#archivio-dialog').dialog( {
						autoOpen: false,
						resizable: false,
						modal: true,
						buttons: {
							Ok: function() {
								fnToggle(oClicked);
								$(this).dialog('close');
							},
							Annulla: function() {
								oClicked = null;
								$(this).dialog('close');
							}
						}
					});
					
					function fnToggle (oObj) {
						oCliked = null;
						src = oObj.attr('src'),
							onCheck = /\_on\.png$/,
							offCheck = /\_off\.png$/;
						$.getJSON('../include/toggle.php', {'sAction': 'toggle',
															'sField': oObj.attr('sField'),
															'sValue': (src.match(onCheck) ? 0 : 1),
															'iView': '$iView',
															'sTable': '',
															'sViewItemLink': '$sViewItemLink',
															'sAPP': '$sAPP'	}, function (json) {
							if (json[0].sAction=='toggle' && json[0].sError=='') { 
								if(src.match(onCheck)) {
									oObj.attr('src', src.replace(onCheck, '_off.png'));
								} else if (src.match(offCheck)) {
									oObj.attr('src', src.replace(offCheck, '_on.png'));
								}
							} else { 
								alert ('Error: ' + json[0].sError);
							} 		
						})
					}; " . ($sAction == 'edit' ? "
									
					$('#sUnita').autocomplete({
						source: 'unita_suggestions.php',
						minLength: 2,
						select: function(event, ui) {
							$(this).val(ui.item.value);
							$('#sUnitaEmail').val(ui.item.sEmail);
						}
					});
					
					$('#sFornitore').autocomplete({
						source: 'fornitore_suggestions.php',
						minLength: 2,
						select: function(event, ui) {
							$(this).val(ui.item.value);
						}
					}); " : '') . "
					
				});
				</script> ";
	
		return $sHeadContent;
	}
